<?php
if (!defined('SHARE_INFO')) exit('No direct script access allowed');
class Verify{
	public $cecCode;	// 验证码
	public $width;		// 验证码宽度
	public $height;		// 验证码高度
	public $num;		// 字符数量
	public $font_size;	// 字体
	public $sessionName; // SESSION名称
	public $mode;		// 验证码类型 1为数字，2为字母，3为数字字母组合
	public $verifyCode;
	public $im;
	
	public function __construct($width=80, $height=20, $num=4, $font_size=20, $sessionName='checkcode', $mode=3){
		$this->width = $width;
		$this->height = $height;
		$this->num = $num;
		$this->font_size = $font_size;
		$this->sessionName = $sessionName;
		$this->mode = $mode;
	}
	
	function echoHead(){
		header("Content-type:image/png");
	}
	
	// 生成验证码明文
	function createCode(){
		if ( $this->mode == 1){
			// 全数字
			$str = "1,2,3,4,5,6,7,8,9";
			$list = explode(",", $str);
			$cmax = count($list) - 1;
			for ( $i=0; $i < $this->num; $i++ ){ 
				$randnum = mt_rand(0, $cmax);
				$verifyCode.= $list[$randnum];
			}
			$this->verifyCode = $verifyCode;
		}elseif ( $this->mode == 2 ){
			// 全字线
			$str = "a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z";
			$list = explode(",", $str);
			$cmax = count($list)-1;
			for ( $i=0; $i < $this->num; $i++ ){ 
				$randnum = mt_rand(0, $cmax);
				$verifyCode.= $list[$randnum];
			}
			$this->verifyCode = strtoupper($verifyCode);
		}elseif ( $this->mode == 3 ){
			// 数字字母组合
			$this->verifyCode = strtoupper(substr(md5(rand()), 0, $this->num));
		}else{
			// 数字字母组合
			$this->verifyCode = strtoupper(substr(md5(rand()), 0, $this->num));
		}
		return $this->verifyCode;
	}
	
	/**
	 * 生成验证码图片
	*/
	function createVerify(){
		$im = imagecreate($this->width, $this->height);
		imagecolorallocate($im, 255, 255, 255);
		$this->im = $im;
		return $this->im;
	}
	
	// 加入干扰素
	function setInterferon(){
		$white = imagecolorallocate($this->im, 234, 185, 95);
		imagearc($this->im, 150, 8, 20, 20, 75, 170, $white);
		imagearc($this->im, 180, 7, 50, 30, 75, 175, $white);
		imageline($this->im, 20, 20, 180, 30, $white);
		imageline($this->im, 20, 18, 170, 50, $white);
		imageline($this->im, 25, 50, 80, 50,$white);
	}
	
	// 杂点
	function setNoise(){
		$num = 100;	// 杂点数量
		$noise_color = imagecolorallocate($this->im,0x00,0x00,0x00);
		for($i=0; $i<$num; $i++){
			//在一个坐标点上画一个单一像素,这个点上面定义了，是黑色的。
			imagesetpixel($this->im, mt_rand(0,$this->width),mt_rand(0,$this->height),$noise_color);
		}
	}
	
	// 设置线条干扰
	function setLine(){
		$num = 10;	//线条数量
		for($i=0; $i<$num; $i++){
			$line_color=imagecolorallocate($this->im,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));
			//在两个坐标点间画一条线，颜色在上面定义
			imageline($this->im, mt_rand(0,$this->width), mt_rand(0,$this->height),mt_rand(0,$this->width),mt_rand(0,$this->height),$line_color);
		}
	}
	
	// 写入验证码
	function writeVerify(){
		// 字体文件
		$a = dirname(__FILE__);
		$font = ROOT_PATH."Public/font/courbd.ttf";
		//$font = '/Public/font/courbd.ttf';
		for ($i=0; $i<$this->num; $i++){
			$bgColor = imagecolorallocate ($this->im, rand(10, 255), rand(0, 128), rand(0, 255));
			if ( empty($bgColor) ){
				// 如果为白色，重新生成
				$bgColor = imagecolorallocate ($this->im, rand(10, 255), rand(0, 128), rand(0, 255));
			}
			// X轴位置
			$x = ($this->width/$this->num)*$i+3;
			// Y轴位置
			$y = $this->height-(($this->height-$this->font_size)/2);
			imagettftext($this->im, rand($this->font_size-3,$this->font_size), rand(0,20), $x, $y, $bgColor, $font, $this->verifyCode[$i]);
		}
	}
	/**
	 * 把验证码的值写入session
	 *
	 * @param string $sessionName
	 */
	function writeSession($sessionName) {
		//session_start();
		session_register($sessionName);
		// 把验证码全部设为小写
		$_SESSION[$sessionName] = md5(strtolower($this->verifyCode));
	}
	

	function outCode(){
		$this->echoHead();
		$this->createCode();
		$this->createVerify();
		$this->setInterferon();
		$this->setNoise();
		$this->setLine();
		$this->writeVerify();
		$this->writeSession("checkcode");
		imagepng($this->im);
		imagedestroy($this->im);
	}
}